var pageParam;
apiready = function() {
	pageParam = api.pageParam; //比如： {"name" : "tans-con"}
	var payAmount = pageParam.payAmount;
	var orderNo = pageParam.orderNo;
	$('#orderNo').text('订单编号：' + orderNo);
	$('#payAmount').text('应付金额：￥' + payAmount);
};

/**
 * [dopay description]
 * @return {[type]} [description]
 */
function dopay(index) {
	if (!index) {
		index = $("#payfs").val();
	}
	console.error(index);
	api.showProgress({
		style: 'default',
		animationType: 'fade',
		title: '支付加载中',
		text: '先喝杯茶...',
		modal: false
	});
	switch (index) {
		case 2:
			{
				wxPay();

				break;
			}
		case 3:
			{
				//支付宝  手机版
				//if(confirm("支付宝(手机版)处于调试阶段,可能无法完成支付,是否继续?")){
				// $("#payfrom").attr('action', '/alipay/wap');
				alipay();

				break;
			}
		default:
			{
				api.hideProgress();
				$api.toastMsg("支付失败");
			}
	}
}


function alipay() {
	var aliPay = api.require('aliPay');
	aliPay.pay({
		subject: '支付',
		body: '支付宝支付',
		amount: pageParam.payAmount,
		// amount: 0.01,
		tradeNO: pageParam.orderNo
	}, function(ret, err) {
		api.hideProgress();
		switch (ret.code) {
			case '9000':
				{
					api.alert({
						title: '支付结果',
						msg: '支付成功',
						buttons: ['确定']
					});
					api.closeWin();
					break;
				}
			case '6001':
				{

					$api.toastMsg("支付已取消");
					break;
				}
			default:
				{
					api.alert({
						title: '支付结果',
						msg: ret.code + " 错误内容：" + err,
						buttons: ['确定']
					});
				}

		}
	});

}

function wxPay() {
	$api.do_get('/wxpay/app/order?orderNo=' + pageParam.orderNo, function(ret, err) {
		//			alert(JSON.stringify(ret));
		$api.log(ret);
		if (ret) {
			var wxPay = api.require('wxPay');

			wxPay.payOrder({
				apiKey: ret.appid,
				orderId: ret.prepayid,
				mchId: ret.partnerid,
				nonceStr: ret.noncestr,
				timeStamp: ret.timestamp,
				'package': 'Sign=WXPay',
				sign: ret.sign
			}, function(ret, err) {
				api.hideProgress();
				if (ret.status) {
					//支付成功
					api.alert({
						title: '支付结果',
						msg: '支付成功',
						buttons: ['确定']
					});

					api.closeWin();
				} else {
					if (err.code == -2) {
						api.alert({
							title: '支付结果',
							msg: '取消支付',
							buttons: ['确定']
						});
					} else {
						$api.log(ret);
						api.alert({
							title: '支付结果',
							msg: '支付失败',
							buttons: ['确定']
						});
					}
				}
			});


		} else {
			api.hideProgress();
			alert(err);
			api.toast({
				msg: '下单失败',
				duration: 1000,
				location: 'top'
			});
		}
	});
}